import com.Dao.UserDao;
import com.javasm.entity.User;
import com.javasm.utils.MybatisHealper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

public class mybatistest {


    @Test
    public  void getUserByIDTest(){
        //启动mybatis框架
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            User o = (User)sqlSession.selectOne("aa.bb.getuserbyid");
            System.out.println(o);
            sqlSession.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }



    @Test
    public  void addUserByIDTest(){
        //启动mybatis框架
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            User user = new User(6,"66",54,"41456");

            int insert = sqlSession.insert("aa.bb.addUser", user);
            System.out.println(insert);
            sqlSession.commit();

            sqlSession.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }



    @Test
    public  void updateUserByIDTest(){
        //启动mybatis框架
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            User user = new User();
            user.setId(4);
            user.setUsername("111");
            user.setPassword("222");
            int update1 = sqlSession.update("aa.bb.updateuser", user);
            System.out.println(update1);

            sqlSession.commit();

            sqlSession.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }


    @Test
    public  void deleteUserByIDTest(){
        //启动mybatis框架
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();

            int delete1 = sqlSession.delete("aa.bb.delete1", 4);
            System.out.println(delete1);

            sqlSession.commit();

            sqlSession.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Test
        public  void tests(){
        SqlSession getsqlsession = MybatisHealper.getsqlsession();
        UserDao userDaoImpl = getsqlsession.getMapper(UserDao.class);
        User    inpuuser    = new User(1115,"2",1,"4");
        int addUser1 = userDaoImpl.addUser(inpuuser);
        System.out.println(addUser1);
        MybatisHealper.backAndSaveSqlSession(getsqlsession);


    }

}
